package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.OrderRefunds;
import com.example.mall.domain.ProductInventory;
import com.example.mall.dto.OrderRefundsDto;
import com.example.mall.dto.ProductInventoryDto;
import com.example.mall.mapper.OrderRefundsMapper;
import com.example.mall.query.OrderRefundsQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.query.ProductInventoryQuery;
import com.example.mall.service.IOrderRefundsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单退款表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-09
 */
@Service
public class OrderRefundsServiceImpl extends ServiceImpl<OrderRefundsMapper, OrderRefunds> implements IOrderRefundsService {

    @Autowired
    private OrderRefundsMapper orderRefundsMapper;

    @Override
    public PageDTO<OrderRefundsDto> queryPage(OrderRefundsQuery orderRefundsQuery) {
        if (orderRefundsQuery == null) {
            orderRefundsQuery = new OrderRefundsQuery();
        }
        Page<OrderRefunds> page = orderRefundsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<OrderRefunds> cartPage = lambdaQuery()
                .like(orderRefundsQuery.getRefundId()!= null, OrderRefunds::getRefundId, orderRefundsQuery.getRefundId())
                .like(orderRefundsQuery.getOrderId()!= null, OrderRefunds::getOrderId, orderRefundsQuery.getOrderId())
                .like(orderRefundsQuery.getRefundAmount()!= null, OrderRefunds::getRefundAmount, orderRefundsQuery.getRefundAmount())
                .like(orderRefundsQuery.getRefundReason()!= null, OrderRefunds::getRefundReason, orderRefundsQuery.getRefundReason())
                .like(orderRefundsQuery.getRefundStatus()!= null, OrderRefunds::getRefundStatus, orderRefundsQuery.getRefundStatus())
                .orderByDesc(OrderRefunds::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, OrderRefundsDto.class);
    }

    @Override
    public OrderRefunds queryOrderRefundsById(Long refundId) {
        AssertUtil.isNotNull(refundId, "订单退款ID不能为空");
        return orderRefundsMapper.selectById(refundId);
    }

    @Override
    public List<OrderRefunds> queryOrderRefundsList() {
        return this.list();
    }

    @Override
    public void update(OrderRefundsDto orderRefundsDto) {
        AssertUtil.isNotNull(orderRefundsDto.getRefundId(), "订单退款ID不能为空");
        OrderRefunds orderRefunds = orderRefundsMapper.selectById(orderRefundsDto.getRefundId());
        AssertUtil.isNotNull(orderRefunds, "订单退款不存在");
        CopyUtils.copyProperties(orderRefundsDto, orderRefunds);
        orderRefunds.setUpdateDate(new Date());
        orderRefundsMapper.updateById(orderRefunds);
    }

    @Override
    public void delete(Long refundId) {
        AssertUtil.isNotNull(refundId, "订单退款ID不能为空");
        OrderRefunds orderRefunds = orderRefundsMapper.selectById(refundId);
        AssertUtil.isNotNull(orderRefunds, "订单退款不存在");
        orderRefunds.setRefundStatus(BaseConstants.CANCEL);
        orderRefunds.setUpdateDate(new Date());
        orderRefundsMapper.updateById(orderRefunds);
    }

    @Override
    public void deleteBatch(List<Long> refundIds) {
        AssertUtil.isNotNull(refundIds, "订单退款ID列表不能为空");
        List<OrderRefunds> orderRefunds = orderRefundsMapper.selectBatchIds(refundIds);
        AssertUtil.isNotNull(orderRefunds, "订单退款不存在");
        for (OrderRefunds orderRefund : orderRefunds) {
            orderRefund.setRefundStatus(BaseConstants.CANCEL);
            orderRefund.setUpdateDate(new Date());
            orderRefundsMapper.updateById(orderRefund);
        }
    }

    @Override
    public void insert(OrderRefundsDto orderRefundsDto) {
        OrderRefunds orderRefunds = new OrderRefunds();
        CopyUtils.copyProperties(orderRefundsDto, orderRefunds);
        orderRefunds.setRefundId(CodeGenerateUtils.generateUniqueId());
        orderRefunds.setCreateDate(new Date());
        orderRefunds.setUpdateDate(new Date());
        orderRefunds.setRefundStatus(BaseConstants.NOT_USED);
        orderRefundsMapper.insert(orderRefunds);
    }
}
